home *** CD-ROM | disk | FTP | other *** search
- { File OPTCASE.PAS: Testing Delphi 2's code optimiser }
- { When there are 4 or less constants it uses dec, je sequences }
- case a of
- 1: a:= 1;
- 2: a:= 1;
- 3: a:= 1;
- 4: a:= 1; { This generates 4 dec <<reg>>, je sequences }
- end;
- { There must be 5 or more constants before the optimizer will use a jump table }
- case a of
- 1: a:= 1;
- 2: a:= 1;
- 3: a:= 1;
- 4: a:= 1;
- 5: a:= 1; { This generates a jump table }
- end;
- { The sequence does not have to be sorted: }
- case a of
- 1: a:= 1;
- 2: a:= 1;
- 3: a:= 1;
- 5: a:= 1;
- 4: a:= 1; { This still generates a jump table }
- end;
- { The sequence does not have to be continuous: }
- case a of
- 1: a:= 1;
- 2: a:= 1;
- 3: a:= 1;
- 6: a:= 1; { This still generates a jump table }
- 4: a:= 1;
- end;
- { When there are big steps in the sequence, the optimizer goes
- back to dec reg, je sequences; when the list is long it divides it
- into pieces by testing with jg etc }
- case a of
- 10: a:= 1;
- 20: a:= 1;
- 30: a:= 1;
- 40: a:= 1;
- 50: a:= 1;
- end;
- { It handles offsets by subtracting before jumping }
- case a of
- 10: a:= 1;
- 11: a:= 1;
- 12: a:= 1;
- 13: a:= 1;
- 14: a:= 1; { This still generates a jump table }
- end;
- { Gaps of one are included in the jump table }
- case a of
- 10: a:= 1;
- 12: a:= 1;
- 14: a:= 1;
- 16: a:= 1;
- 18: a:= 1; { This still generates a jump table }
- end;
- { When the gaps become too large (ie it would waste to much space
- on dummy pointers) it creates a secondary array to convert from the
- case-constants to jump-table indexes! The first level converts 10,
- 12, 14, 16 and 23 into 1, 2, 3, 4 and 5, respectively. All other
- numbers convert into 0. This new index is used in a jump table
- that only consist of 6 entries (0..5) }
- case a of
- 10: a:= 1;
- 12: a:= 1;
- 14: a:= 1;
- 16: a:= 1;
- 23: a:= 1; { Generates a two-level jump table! }
- end;
- { When the gap becomes too large even for the secondary table, it
- goes back to the dec, je algorithm again }
- case a of
- 10: a:= 1;
- 12: a:= 1;
- 14: a:= 1;
- 16: a:= 1;
- 100: a:= 1;
- end;
- { When you have a very long case and not all of it can be turned
- into a jump table, the optimizer divides it into several parts and
- generates a jump table for the parts that can have it. It does not
- seem to optimize the exact size of these parts, however... }
- case a of
- 1: a:= 1;
- 2: a:= 1;
- 3: a:= 1;
- 4: a:= 1;
- 5: a:= 1;
- 6: a:= 1;
- 7: a:= 1;
- 8: a:= 1;
- 9: a:= 1;
- 10: a:= 1; { There is a jump table for entries 1..10 }
- 11: a:= 1;
- 12: a:= 1;
- 13: a:= 1;
- 14: a:= 1;
- 15: a:= 1;
- 16: a:= 1;
- 17: a:= 1;
- 18: a:= 1;
- 100: a:= 1; { There are dec/sub, je for the rest }
- end;